<?php
/**
 * Native（原生）支付-模式二-demo
 * ====================================================
 * 商户生成订单，先调用统一支付接口获取到code_url，
 * 此URL直接生成二维码，用户扫码后调起支付。
 * 
*/
header('Content-type:text/html; charset=utf-8');

//订单号
$out_trade_no = trim($_GET['ordercode']);
//验证规范性
if(strlen($out_trade_no)!=18) {
	echo '<script type="text/javascript">alert("信息错误！");history.go(-1)</script>';
	exit;
}

include '/webHome/host2604792/www'.'/YunPHP/class/YP.class.php';
$pdo = YP::initPDO();

//订单类型
$type = substr($out_trade_no,0,2);
//会员卡订单
if($type=='10') {
	$st = $pdo->prepare('select b.cardname as title,a.payprice,a.time_add from yp_order_card a 
		inner join yp_vipcard b on a.cardid=b.id 
		where a.ordercode=?');
	$st->execute(array($out_trade_no));
	$orderinfo = $st->fetch();
}
//产品订单
else if($type=='20'){
	$st = $pdo->prepare('select title,payprice,time_add from yp_order where ordercode=?');
	$st->execute(array($out_trade_no));
	$orderinfo = $st->fetch();
}else {
	echo '<script type="text/javascript">alert("信息错误！");history.go(-1)</script>';
	exit;
}

include_once("WxPayPubHelper/WxPayPubHelper.php");

//使用统一支付接口
$unifiedOrder = new UnifiedOrder_pub();
	
$unifiedOrder->setParameter("body",$orderinfo['title']);//商品描述
$unifiedOrder->setParameter("out_trade_no",$out_trade_no);//商户订单号 
$unifiedOrder->setParameter("total_fee",floatval($orderinfo['payprice'])*100);//总金额
$unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址 
$unifiedOrder->setParameter("trade_type","NATIVE");//交易类型
//非必填参数，商户可根据实际情况选填
//$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号  
//$unifiedOrder->setParameter("device_info","XXXX");//设备号 
//$unifiedOrder->setParameter("attach","XXXX");//附加数据 
//$unifiedOrder->setParameter("time_start","XXXX");//交易起始时间
//$unifiedOrder->setParameter("time_expire","XXXX");//交易结束时间 
//$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记 
//$unifiedOrder->setParameter("openid","XXXX");//用户标识
//$unifiedOrder->setParameter("product_id","XXXX");//商品ID
	
//获取统一支付接口结果
$unifiedOrderResult = $unifiedOrder->getResult();

//商户根据实际情况设置相应的处理流程
if ($unifiedOrderResult["return_code"] == "FAIL") {
	//商户自行增加处理流程
	echo "通信出错：".$unifiedOrderResult['return_msg']."<br>";
}
elseif($unifiedOrderResult["result_code"] == "FAIL") {
	//商户自行增加处理流程
	echo "错误代码：".$unifiedOrderResult['err_code']."<br>";
	echo "错误代码描述：".$unifiedOrderResult['err_code_des']."<br>";
}
elseif($unifiedOrderResult["code_url"] != NULL) {
	//从统一支付接口获取到code_url
	$code_url = $unifiedOrderResult["code_url"];
	//商户自行增加处理流程
	//......
}

?>


<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>微信安全支付</title>
<link rel="stylesheet" type="text/css" href="/com/css/ty.css" />
</head>
<body>
	<div class="view">
	<?php include rootPath.'YunPHP/template/Topmenu.html';?>
	<div class="mt20 menu_div"><?php include 'menu.html';?></div>
		<div class="w_title"><img src="/com/imgs/min_wx.png" />微信支付</div>
		<div class="ewm_div">
			<div class="paycode" id="qrcode"></div>
			<div class="smts"><img src="/com/imgs/sm_btn.png" /></div>
			<div class="zf_div">支付金额：￥<?=floatval($orderinfo['payprice'])?>元</div>
			<div class="zf_box">
				<?=$orderinfo['title']?>
				<div class="lin">交易订单号：<i class="fr"><?=$out_trade_no?></i></div>
				<div class="lin">创建时间：<i class="fr"><?=date('Y/m/d H:i',$orderinfo['time_add'])?></i></div>
			</div>
			<a class="fk_btn" onClick="isFinishPay()">付款已完成</a>
		</div>
	</div>
</body>
<script type="text/javascript" src="./qrcode.js"></script>
<script type="text/javascript" src="/com/js/jquery.js"></script>
<script type="text/javascript">
if(<?php echo $unifiedOrderResult["code_url"] != NULL; ?>){
	var url = "<?php echo $code_url;?>";
	//参数1表示图像大小，取值范围1-10；参数2表示质量，取值范围'L','M','Q','H'
	var qr = qrcode(10, 'M');
	qr.addData(url);
	qr.make();
	var code=document.createElement('DIV');
	code.innerHTML = qr.createImgTag();
	var element=document.getElementById("qrcode");
	element.appendChild(code);
}
//是否完成付款
function isFinishPay(){
$.post("/index.php?m=index&c=isFinishPay",{code:'<?=$out_trade_no?>'},function(data){
	if(data=="payed"){
		window.location = "/member.php?m=member&view=<?=$type=='10'?'member_orderlist_card':'member_orderlist'?>";
		return;
	}else if(data=="no_pay"){
		alert("您还未完成支付，请用微信扫描屏幕上的二维码！");
	}
});
}
</script>
</html>